<?php

namespace app\admin\controller\crm;

use app\common\controller\Backend;
use \Think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Buserinfo extends Backend
{
    
    /**
     * Buserinfo模型对象
     * @var \app\admin\model\Buserinfo
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Buserinfo;

    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 工商信息
     * @param string $ids
     * @return string|\think\response\Jsons
     */
    public function index($ids=''){
        if(empty($ids)){
            $this->error(__('非法操作'));
        }
        $linkAllot = Db::name('Allot');
        $allotData = $linkAllot->where(array('id'=>$ids))->find();
        if(!$allotData){
            $this->error('分配客户不存在');
        }
        //调用抓取功能，抓取最新的用户数据
        $queryController = new \app\admin\controller\crm\Query();
        $queryController->index($allotData['buser_id']);
        $data = Db::name('Buserinfo')->where(array('buser_id'=>$allotData['buser_id']))->find();
        if(!$data){
            $this->error('没有当前用户的工商数据');
        }
        $composite = array();
        if($data['composite']){
            $composite = json_decode($data['composite'],true);

        }
        $data['composite'] = $composite;
        $healthy = array();
        if($data['healthy']){
            $healthy = json_decode($data['healthy'],true);
        }
        $data['healthy'] = $healthy;
        $circles = array();
        if($data['circles']){
            $circles = json_decode($data['circles'],true);
        }
        $data['circles'] = $circles;
        $organization = array();
        if($data['organization']){
            $organization = json_decode($data['organization'],true);
        }
        $data['organization'] = $organization;
        $right = array();
        if($data['right']){
            $right = json_decode($data['right'],true);
        }
        $data['right'] = $right;
        $this->view->assign('data',$data);
        return $this->view->fetch();
    }

    /**
     * 舆情数据
     * @param string $ids
     */
    public function sentiment($ids=''){
        $this->searchFields = 'id,title,url';//自定义搜索字段
        if(empty($ids)){
            $this->error(__('非法操作'));
        }
        $linkAllot = Db::name('Allot');
        $allotData = $linkAllot->where(array('id'=>$ids))->find();
        //查询当前客户名字
        $linkBuser = Db::name('Buser');
        $buserData = $linkBuser->where(array('id'=>$allotData['buser_id']))->find();
        if(!$buserData){
            $this->error(__('不存在当前客户'));
        }
        if($this->request->isAjax()){
            $link = Db::connect([
                'type'        => 'mysql',
                // 数据库连接DSN配置
                'dsn'         => '',
                // 服务器地址
                'hostname'    => '116.62.78.18',
                // 数据库名
                'database'    => 'tw_onesh_net',
                // 数据库用户名
                'username'    => 'tw_onesh_net',
                // 数据库密码
                'password'    => 'nFyksTjZnEiCMSaw',
                // 数据库连接端口
                'hostport'    => '',
                // 数据库连接参数
                'params'      => [],
                // 数据库编码默认采用utf8
                'charset'     => 'utf8',
                // 数据库表前缀
                'prefix'      => 'cmf_',
            ]);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
          /*  $oneWhere = array('title'=>array('LIKE','%'.$buserData['username']));*/
            $count = $link->name('Opinion')->where($where)->where('LOCATE("'.$buserData['username'].'",`title`)>0')->count();
            $data = $link->name('Opinion')->where($where)->where('LOCATE("'.$buserData['username'].'",`title`)>0')
                    ->order($sort, $order)->limit($offset, $limit)->select();
            $result = array("total" => $count, "rows" => $data);
            return json($result);
        }
        $this->view->assign('ids',$ids);
        return $this->view->fetch();
    }
}
